unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  n=30;

type
  TForm1 = class(TForm)
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  arr : array [0..n*n-1] of TButton;
  turn : integer;

implementation

{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);
var
  i, j, a, b: integer;
begin
  for i:=0 to n*n-1 do
  begin
    if (Sender = arr[i]) then
    begin
      if (turn mod 2 = 0) then
      begin
        arr[i].Caption:='X';
      end else begin
        arr[i].Caption:='O';
      end;
      arr[i].Enabled:=False;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        a:=a+1;
        if (j mod n) <> 0 then
        begin
          j:=j-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        b:=b+1;
        if (j mod n) <> 29 then
        begin
          j:=j+1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        Application.MessageBox('X wins',' ');
        for j:=0 to n*n-1 do
        begin
          arr[j].Caption:= ' ';
          arr[j].Enabled:=True;
        end;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        a:=a+1;
        if (j mod n) <> 0 then
        begin
          j:=j-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        b:=b+1;
        if (j mod n) <> 29 then
        begin
          j:=j+1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        Application.MessageBox('O wins!',' ');
        for j:=0 to n*n-1 do
        begin
          arr[j].Caption:= ' ';
          arr[j].Enabled:=True;
        end;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        a:=a+1;
        j:=j-n;
      end;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        b:=b+1;
        j:=j+n;
      end;
      if (a+b+1 >= 5) then
      begin
        Application.MessageBox('O wins!', 'Congratulations!');
        for j:=0 to n*n-1 do
        begin
          arr[j].Caption:= ' ';
          arr[j].Enabled:=True;
        end;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        a:=a+1;
        j:=j-n;
      end;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        b:=b+1;
        j:=j+n;
      end;
      if (a+b+1 >= 5) then
      begin
        Application.MessageBox('X wins!', 'Congratulations!');
        for j:=0 to n*n-1 do
        begin
          arr[j].Caption:= ' ';
          arr[j].Enabled:=True;
        end;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        a:=a+1;
        if (j mod n) <> 0 then
        begin
          j:=j-n-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        b:=b+1;
        if (j mod n) <> n-1 then
        begin
          j:=j+n+1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        Application.MessageBox('X wins!', 'Congratulations!');
        for j:=0 to n*n-1 do
        begin
          arr[j].Caption:= ' ';
          arr[j].Enabled:=True;
        end;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        a:=a+1;
        if (j mod n) <> 0 then
        begin
          j:=j-n-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        b:=b+1;
        if (j mod n) <> n-1 then
        begin
          j:=j+n+1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        Application.MessageBox('O wins!', 'Congratulations!');
        for j:=0 to n*n-1 do
        begin
          arr[j].Caption:= ' ';
          arr[j].Enabled:=True;
        end;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        a:=a+1;
        if (j mod n) <> 0 then
        begin
          j:=j+n-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'X') do
      begin
        b:=b+1;
        if (j mod n) <> n-1 then
        begin
          j:=j-n-1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        Application.MessageBox('X wins!', 'Congratulations!');
        for j:=0 to n*n-1 do
        begin
          arr[j].Caption:= ' ';
          arr[j].Enabled:=True;
        end;
      end;
      a:=-1;
      b:=-1;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        a:=a+1;
        if (j mod n) <> 0 then
        begin
          j:=j+n-1;
        end else begin
          break;
        end;
      end;
      j:=i;
      while (arr[j].Caption = 'O') do
      begin
        b:=b+1;
        if (j mod n) <> n-1 then
        begin
          j:=j-n-1;
        end else begin
          break;
        end;
      end;
      if (a+b+1 >= 5) then
      begin
        Application.MessageBox('O wins!', 'Congratulations!');
        for j:=0 to n*n-1 do
        begin
          arr[j].Caption:= ' ';
          arr[j].Enabled:=True;
        end;
      end;
    end;
  end;
  turn:=turn+1;
end;




procedure TForm1.Button2Click(Sender: TObject);
var
  i, a: integer;
begin
  for i:=0 to n*n-1 do
  begin
    arr[i]:= TButton.Create(Form1);
    arr[i].Width:=20;
    arr[i].Height:=20;
    arr[i].Left:= 50 + (i mod n)*20;
    arr[i].Top:= 50 + (i div n)*20;
    arr[i].Parent:=Form1;
    arr[i].OnClick:=Button1Click;
  end;
end;


end.
